﻿using BigEgg.Framework.Applications;
using BigEgg.Framework.Applications.ViewModels;
using FMStudio.Documents.TemplateDocument;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Windows.Input;

namespace FMStudio.Templates.DesignTool.Application.Services
{
    [Export(typeof(IFileService)), Export]
    public class FileService : DataModel, IFileService
    {
        #region Fields
        private readonly ObservableCollection<TemplateDocument> documents;
        private readonly ReadOnlyObservableCollection<TemplateDocument> readOnlyDocuments;
        private TemplateDocument activeDocument;

        private RecentFileList recentFileList;
        private ICommand newCommand;
        private ICommand openCommand;
        private ICommand saveCommand;
        private ICommand saveAsCommand;
        private ICommand closeCommand;
        #endregion

        [ImportingConstructor]
        public FileService()
        {
            this.documents = new ObservableCollection<TemplateDocument>();
            this.readOnlyDocuments = new ReadOnlyObservableCollection<TemplateDocument>(documents);
        }

        #region Properties
        public ReadOnlyObservableCollection<TemplateDocument> Documents { get { return this.readOnlyDocuments; } }

        public TemplateDocument ActiveDocument 
        {
            get { return this.activeDocument; }
            set
            {
                if (this.activeDocument != value)
                {
                    this.activeDocument = value;
                    RaisePropertyChanged("ActiveDocument");
                }
            }
        }

        public RecentFileList RecentFileList
        {
            get { return this.recentFileList; }
            set
            {
                if (this.recentFileList != value)
                {
                    this.recentFileList = value;
                    RaisePropertyChanged("RecentFileList");
                }
            }
        }

        public ICommand NewCommand
        {
            get { return this.newCommand; }
            set
            {
                if (this.newCommand != value)
                {
                    this.newCommand = value;
                    RaisePropertyChanged("NewCommand");
                }
            }
        }

        public ICommand OpenCommand
        {
            get { return this.openCommand; }
            set
            {
                if (this.openCommand != value)
                {
                    this.openCommand = value;
                    RaisePropertyChanged("OpenCommand");
                }
            }
        }

        public ICommand SaveCommand
        {
            get { return this.saveCommand; }
            set
            {
                if (this.saveCommand != value)
                {
                    this.saveCommand = value;
                    RaisePropertyChanged("SaveCommand");
                }
            }
        }

        public ICommand SaveAsCommand
        {
            get { return this.saveAsCommand; }
            set
            {
                if (this.saveAsCommand != value)
                {
                    this.saveAsCommand = value;
                    RaisePropertyChanged("SaveAsCommand");
                }
            }
        }

        public ICommand CloseCommand
        {
            get { return this.closeCommand; }
            set
            {
                if (this.closeCommand != value)
                {
                    this.closeCommand = value;
                    RaisePropertyChanged("CloseCommand");
                }
            }
        }
        #endregion

        #region Methods
        #region Public Methods
        public void AddDocument(TemplateDocument document)
        {
            if (document == null) { throw new ArgumentException("document"); }
            this.documents.Add(document);
        }

        public void RemoveDocument(TemplateDocument document)
        {
            if (document == null) { throw new ArgumentException("document"); }
            if (!this.documents.Contains(document)) { throw new ArgumentException("document"); }
            this.documents.Remove(document);
        }
        #endregion
        #endregion
    }
}
